<?php

require_once ABSPATH . '/system/sql.php';
require_once ABSPATH . '/system/class_image.php';

$sql_link = MySQL::getInstance();
$identity = $GLOBALS['user']['identity'];

if($_GET['action'] == "list"){
    $page = $_GET['page'];
    $size = $_GET['size'];
    $center_id = $_GET['center'];

    $res = $sql_link->prepare('SELECT room_id,name,img_src FROM rooms WHERE center_id=:center_id OR 0 = :center_id');
    $res->bindParam(':center_id',$center_id);
    $res->execute();
    $res_data = $res->fetchAll(PDO::FETCH_ASSOC);

    $all_len = count($res_data);

    $ret_data = array_slice($res_data, ($page-1)*$size,$size);

    $ret = array(
        'code' => 200,
        'msg' => 'ok',
        "pages"=> ceil($all_len / $size),
        "data"=> $ret_data
    );
}else if($_GET['action'] == "info"){
    $room_id = $_GET['room_id'];
    $res = $sql_link->prepare('SELECT name,center_id,address,introduce FROM rooms WHERE room_id=:room_id');
    $res->bindParam(':room_id',$room_id);
    $res->execute();
    $res_data = $res->fetch(PDO::FETCH_ASSOC);

    $ret_data = $res_data;

    $res = $sql_link->prepare('SELECT sniffer_mac FROM sniffer_infos WHERE room_id=:room_id');
    $res->bindParam(':room_id',$room_id);
    $res->execute();
    $res_data = $res->fetchAll(PDO::FETCH_ASSOC);

    $ret_data['sniffers'] = array_column($res_data,'sniffer_mac');

    $ret = array(
        'code' => 200,
        'msg' => 'ok',
        "data"=> $ret_data
    );
}else if($identity == 'admin' || $identity == 'teacher'){

    if($_POST['action'] == 'mod' || $_POST['action'] == 'add'){

        $_POST['sniffer_mac'] = strtolower($_POST['sniffer_mac']);
        $sniffer_macs = explode(',', rtrim($_POST['sniffer_mac'], ','));
        if($sniffer_macs[0] == ''){
            unset($sniffer_macs[0]);
        }

        $len = count($sniffer_macs);

        if($_POST['action'] == 'mod'){
            $res = $sql_link->prepare('UPDATE rooms SET name = :name, center_id = :center_id,
            address = :address, introduce = :introduce WHERE room_id = :room_id');
            $res->bindParam(':name',$_POST['name']);
            $res->bindParam(':center_id',$_POST['center_id']);
            $res->bindParam(':address',$_POST['address']);
            $res->bindParam(':introduce',$_POST['introduce']);
            $res->bindParam(':room_id',$_POST['room_id']);
            $res->execute();

            $str = 'DELETE FROM sniffer_infos WHERE room_id=? AND sniffer_mac NOT IN(';
            for($i=0;$i<$len;$i++)
            {
                $str.='?,';
            }
            $str = rtrim($str, ',');
            $str = $str.')';
            $res = $sql_link->prepare($str);
            $res->bindValue(1,$_POST['room_id']);
            $index = 2;
            foreach($sniffer_macs as $sniffer_mac){
                $res->bindValue($index,$sniffer_mac);
                $index ++;
            }
            $res->execute();
            
        }else if($_POST['action'] == 'add'){
            $res = $sql_link->prepare('INSERT INTO rooms (name, center_id,address,introduce) VALUE (:name,:center_id,:address,:introduce)');
            $res->bindParam(':name',$_POST['name']);
            $res->bindParam(':center_id',$_POST['center_id']);
            $res->bindParam(':address',$_POST['address']);
            $res->bindParam(':introduce',$_POST['introduce']);
            $res->execute();
            $_POST['room_id'] = $sql_link->lastInsertId();
        }

        $update_res = $sql_link->prepare('UPDATE sniffer_infos SET room_id = :room_id WHERE sniffer_mac = :sniffer_mac');
        $update_res->bindParam(':room_id',$_POST['room_id']);
        $update_res->bindParam(':sniffer_mac',$sniffer_mac);

        $insert_res = $sql_link->prepare('INSERT INTO sniffer_infos (sniffer_mac, room_id) VALUE (:sniffer_mac,:room_id)');
        $insert_res->bindParam(':room_id',$_POST['room_id']);
        $insert_res->bindParam(':sniffer_mac',$sniffer_mac);

        foreach($sniffer_macs as $item){
            $sniffer_mac = $item;
            $update_res->execute();

            if($update_res->rowCount() == 0){
                $insert_res->execute();
            }
        }


        $ret = array(
            'code' => 200,
            'msg' => 'ok',
        );
    }else if($_FILES['file']['error'] != UPLOAD_ERR_NO_FILE && isset($_GET['room_id']) ){

        $save_url = IMAGE::save();
        if($save_url == false){
            $ret = array(
                'code' => 403,
                'msg' => '上传失败',
            );
        }else{

            $res = $sql_link->prepare('SELECT img_src FROM rooms WHERE room_id=:room_id');
            $res->bindParam(':room_id',$_GET['room_id']);
            $res->execute();
            $res_data = $res->fetch(PDO::FETCH_ASSOC);

            unlink(ABSPATH .'/public'.$res_data['img_src']);

            $update_res = $sql_link->prepare('UPDATE rooms SET img_src = :img_src WHERE room_id = :room_id');
            $update_res->bindParam(':img_src',$save_url);
            $update_res->bindParam(':room_id',$_GET['room_id']);
            $update_res->execute();

            $ret = array(
                'code' => 200,
                'msg' => 'ok',
                'data' => $save_url
            );
        }
    }
}

echo json_encode($ret);
/*
$res = $sql_link->prepare('SELECT room_id,name,institute FROM rooms');
$res->execute();
$data = $res->fetchAll(PDO::FETCH_ASSOC);

$ret = array(
    'code' => 200,
    'msg' => 'ok',
    "data"=> $data
);

echo json_encode($ret);

{
	"code": 200,
    "msg": "ok",
    "data": [{
        "institute":"信息科学与工程学院",
        "rooms":[{
            "room_id": 1,
            "name":"创新实验室"
        }]
    }]
}

{
	"code": 200,
    "msg": "ok",
    "pages":1,
    "data": [{
        "room_id": 1,
        "name":"创新实验室"
    }]
}
*/ ?>